package com.fyl.datasource.aop;

import com.fyl.datasource.anno.TargetDataSource;
import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

public class TargetDataSourceAnnotationAdvisor extends AbstractPointcutAdvisor {

    private Advice advice;

    private Pointcut pointcut;

    public TargetDataSourceAnnotationAdvisor(MethodInterceptor advice) {
        this.advice = advice;
        this.pointcut = buildPointcut();
    }

    @Override
    public Pointcut getPointcut() {
        return this.pointcut;
    }

    @Override
    public Advice getAdvice() {
        return this.advice;
    }

//    @Override
//    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
//
//    }

    private Pointcut buildPointcut() {
        // class
        Pointcut cpc = new AnnotationMatchingPointcut(TargetDataSource.class, true);
        // method
        Pointcut mpc = AnnotationMatchingPointcut.forMethodAnnotation(TargetDataSource.class);
        return new ComposablePointcut(cpc).union(mpc);
    }
}
